﻿using System;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using Microsoft.Phone.Controls;

namespace TTApp.Phone.Silverlight.Hexagons
{
    public partial class TwoPlayersGameHoster : UserControl
    {
        #region //--------- Public Constructors ---------//
        /// <summary>
        /// Khởi tạo đối tượng.
        /// </summary>
        public TwoPlayersGameHoster()
        {
            InitializeComponent();
            Loaded += new RoutedEventHandler(OnLoaded);
        }
        #endregion

        #region //--------- CLR Properties --------------//
        /// <summary>
        /// Lấy hoặc gán đối tượng GameBase của hoster này.
        /// </summary>
        public GameBase Game
        {
            get
            {
                if (gamePanel.Children.Count == 0)
                {
                    throw new InvalidOperationException("Chưa có Game.");
                }

                return gamePanel.Children[0] as GameBase;
            }
            set
            {
                GameBase game = value;

                gamePanel.Children.Clear();
                gamePanel.Children.Add(game);

                game.GameStarting += new EventHandler(OnGameStarting);
                game.GameEnded += new EventHandler(OnGameEnded);
            }
        }

        /// <summary>
        /// Lấy đối tượng bảng điểm của người chơi 1.
        /// </summary>
        public OnePlayerScorePanel Player1ScorePanel
        {
            get { return player1ScorePanel; }
        }

        /// <summary>
        /// Lấy đối tượng bảng điểm của người chơi 2.
        /// </summary>
        public OnePlayerScorePanel Player2ScorePanel
        {
            get { return player2ScorePanel; }
        }
        #endregion

        #region //--------- Event Handlers --------------//
        private void OnLoaded(object sender, RoutedEventArgs args)
        {
            if (Game == null)
            {
                throw new Exception("Hoster chưa có Game.");
            }
            
            player1ScorePanel.Game = Game;
            player2ScorePanel.Game = Game;

            resultBoard.Game = Game;
            

            // Bỏ sự kiện drag, pinch nếu disableRect đang visible.
            GestureListener gl = GestureService.GetGestureListener(disableRect);
            gl.Flick += new EventHandler<FlickGestureEventArgs>(OnFlick);
            gl.DragDelta += new EventHandler<DragDeltaGestureEventArgs>(OnDragDelta);
            gl.PinchDelta += new EventHandler<PinchGestureEventArgs>(OnPinchDelta);
        }

        /// <summary>
        /// Bỏ disableRect, resultBoard nếu cần thiết.
        /// </summary>
        private void OnGameStarting(object sender, EventArgs args)
        {
            disableRect.Visibility = Visibility.Collapsed;
            resultBoard.Visibility = Visibility.Collapsed;
        }

        /// <summary>
        /// Hiển thị disableRect, bảng thông báo kết quả...
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="args"></param>
        private void OnGameEnded(object sender, EventArgs args)
        {
            disableRect.Visibility = Visibility.Visible;
            resultBoard.Visibility = Visibility.Visible;
        }

        /// <summary>
        /// Bỏ handler Pinch của viewer.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="args"></param>
        private void OnPinchDelta(object sender, PinchGestureEventArgs args)
        {
            args.Handled = true;
        }

        /// <summary>
        /// Bỏ hanlder Drag của viewer.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="args"></param>
        private void OnDragDelta(object sender, DragDeltaGestureEventArgs args)
        {
            args.Handled = true;
        }

        /// <summary>
        /// Bỏ handler Flick của viewer.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="args"></param>
        private void OnFlick(object sender, FlickGestureEventArgs args)
        {
            args.Handled = true;
        }
        #endregion
    }
}
